package com.xzt.transaction.controller;

import com.xzt.transaction.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userService;

    //事务管理器
    @Autowired
    private DataSourceTransactionManager transactionManager;

    //定义事务属性
    @Autowired
    private TransactionDefinition transactionDefinition;

    @RequestMapping("/registry")
    public String registry(String name, String password) {
        //开启事务
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        //⽤⼾注册
        userService.registryUser(name, password);
        //提交事务
//        transactionManager.commit(transactionStatus);
        //回滚事务
        transactionManager.rollback(transactionStatus);
        return "注册成功";
    }
}
